<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2020/11/12
  Time: 17:34
  To change this template use File | Settings | File Templates.
--%>


<%@ page import="java.sql.ResultSet" %>
<%@ page import="cn.edu.xaut.bean.UserBean" %>
<%@ page import="cn.edu.xaut.db.ConnDB" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>用户信息修改</title>
    <link rel="shortcut icon" href="#">
    <link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.css">
    <script src="bootstrap-3.3.7-dist/js/bootstrap.js"></script>
    <script src="js/jquery-3.5.1.js"></script>
    <script>
        /*校验是否中文名称组成 */
        function ischina(str) {
            var reg = /^[\u4E00-\u9FA5]{2,4}$/; /*定义验证表达式*/
            return reg.test(str); /*进行验证*/
        }

        /*校验电话码格式 */
        function isTelCode(str) {
            var reg = /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/;
            return reg.test(str);
        }

        /*校验邮箱格式*/
        function isEmail(str) {
            var reg = /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/;
            return reg.test(str);
        }
    </script>
</head>
<body>


<%
    UserBean userBean = new UserBean();
    int id = Integer.parseInt(request.getParameter("id"));
    try {
        String sql = "select * from dbmis.tb_student where id = " + id; // 查询数据库
        ResultSet rs = ConnDB.query(sql);

        if (rs.next()) {
            userBean.setId(id);
            userBean.setUsername(rs.getString("username"));
            userBean.setPassword(rs.getString("password"));
            userBean.setRealname(rs.getString("realname"));
            userBean.setTel(rs.getString("tel"));
            userBean.setEmail(rs.getString("email"));
            userBean.setCollege(rs.getString("college"));
        }

        ConnDB.close();     // 释放资源
    } catch (Exception e) {
        e.printStackTrace();
    }
%>

<div class="container">

    <div class="panel panel-primary">

        <div class="panel-heading">
            <h1 class="text-center">学生信息修改</h1>
        </div>

        <div class="panel-body">

            <form class="form-horizontal">

                <div class="form-group">
                    <div class="alert alert-warning alert-dismissible hide" role="alert">
                        <button type="button" class="close fade in" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <strong></strong>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-2 control-label" for="username">用户名</label>
                    <div class="col-sm-10">
                        <input type="text" name="username" id="username" class="form-control" value="<%=userBean.getUsername()%>">
                    </div>
                </div>

                <div class="form-group">
                    <label for="password" class="col-sm-2 control-label">密码</label>
                    <div class="col-sm-10">
                        <input type="password" name="password" id="password" class="form-control" value="<%=userBean.getPassword()%>">
                    </div>
                </div>

                <div class="form-group">
                    <label for="repassword" class="col-sm-2 control-label">确认密码</label>
                    <div class="col-sm-10">
                        <input type="password" name="repassword" id="repassword" class="form-control" value="<%=userBean.getPassword()%>">
                    </div>
                </div>

                <div class="form-group">
                    <label for="realname" class="col-sm-2 control-label">姓名</label>
                    <div class="col-sm-10">
                        <input type="text" name="realname" id="realname" class="form-control" value="<%=userBean.getRealname()%>">
                    </div>
                </div>

                <div class="form-group">
                    <label for="tel" class="col-sm-2 control-label">手机号码</label>
                    <div class="col-sm-10">
                        <input type="text" name="tel" id="tel" class="form-control" value="<%=userBean.getTel()%>">
                    </div>
                </div>

                <div class="form-group">
                    <label for="email" class="col-sm-2 control-label">邮箱</label>
                    <div class="col-sm-10">
                        <input type="email" name="email" id="email" class="form-control" value="<%=userBean.getEmail()%>">
                    </div>
                </div>

                <div class="form-group">
                    <label for="college" class="col-sm-2 control-label">所在学院</label>
                    <div class="col-sm-10">
                        <input type="text" name="college" id="college" class="form-control" value="<%=userBean.getCollege()%>">
                    </div>
                </div>

                <div class="form-group text-center">
                    <button type="button" class="btn btn-info" id="save">保存</button>
                    <button type="button" class="btn btn-info" id="back">返回</button>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    $(function () {
        $("#save").click(function () {
            if (confirm("确认保存修改？")) {
                var username = $("#username").val();
                var password = $("#password").val();
                var realname = $("#realname").val();
                var tel = $("#tel").val();
                var email = $("#email").val();
                var college = $("#college").val();
                window.location.href = ("modifyProcess?username=" + username + "&password=" +
                    password + "&realname=" + realname + "&tel=" + tel + "&email=" + email +
                    "&college=" + college + "&id=" + <%=id%>);
            }
        });

        $("#back").click(function () {
            window.location.href = "manage.jsp";
        });

        $("#username").blur(function () {
            var username = $("#username").val();
            if (username == "") {
                $(".alert-warning").html("<strong>警告：</strong>用户名不能为空！");
                $(".alert-warning").removeClass("hide");
            } else {
                $(".alert-warning").addClass("hide");
            }
        });

        $("#password").blur(function () {
            var password = $("#password").val();
            if (password == "") {
                $(".alert-warning").html("<strong>警告：</strong>密码不能为空！");
                $(".alert-warning").removeClass("hide");
            } else {
                $(".alert-warning").addClass("hide");
            }
        });

        $("#repassword").blur(function () {
            var repassword = $("#repassword").val();
            var password = $("#password").val();
            if (repassword == "") {
                $(".alert-warning").html("<strong>警告：</strong>请确认密码！");
                $(".alert-warning").removeClass("hide");
            } else if (repassword !== password) {
                $(".alert-warning").html("<strong>警告：</strong>两次密码输入不一致！");
                $(".alert-warning").removeClass("hide");
            } else {
                $(".alert-warning").addClass("hide");
            }
        });

        $("#realname").blur(function () {
            var realname = $("#realname").val();
            if (realname == "") {
                $(".alert-warning").html("<strong>警告：</strong>请输入姓名！");
                $(".alert-warning").removeClass("hide");
            } else {
                $(".alert-warning").addClass("hide");
            }
        });

        $("#tel").blur(function () {
            var tel = $("#tel").val();
            if (tel == "") {
                $(".alert-warning").html("<strong>警告：</strong>请输入手机号！");
                $(".alert-warning").removeClass("hide");
            } else if (!isTelCode(tel)) {
                $(".alert-warning").html("<strong>警告：</strong>手机号码格式错误！");
                $(".alert-warning").removeClass("hide");
            } else {
                $(".alert-warning").addClass("hide");
            }
        });

        $("#email").blur(function () {
            var email = $("#email").val();
            if (email == "") {
                $(".alert-warning").html("<strong>警告：</strong>请输入邮箱！");
                $(".alert-warning").removeClass("hide");
            } else if (!isEmail(email)) {
                $(".alert-warning").html("<strong>警告：</strong>邮箱格式错误！");
                $(".alert-warning").removeClass("hide");
            } else {
                $(".alert-warning").addClass("hide");
            }
        });

        $("#college").blur(function () {
            var college = $("#college").val();
            if (college == "") {
                $(".alert-warning").html("<strong>警告：</strong>请输入学院！");
                $(".alert-warning").removeClass("hide");
            } else {
                $(".alert-warning").addClass("hide");
            }
        });
    })
</script>
</body>
</html>